package org.example.demo1.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.example.demo1.common.PageData;
import org.example.demo1.common.PageQuery;
import org.example.demo1.common.ResponseData;
import org.example.demo1.domain.UserInfo;
import org.example.demo1.mapper.UserMapper;
import org.example.demo1.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    String symbol = "^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$%^&*()\\-_=+{};:,<.>\\/?]).{6,}$";

    @Override
    public ResponseData<UserInfo> addUser(UserInfo userInfo) {
        if(StrUtil.isBlank(userInfo.getUserName())){
            return ResponseData.fail("用户名不能为空");
        }
        if(StrUtil.isBlank(userInfo.getPassword())){
            return ResponseData.fail("密码不能为空");
        }
        if(StrUtil.isBlank(userInfo.getPhone())){
            return ResponseData.fail("手机号不能为空");
        }
        if(StrUtil.isBlank(userInfo.getEmail())){
            return ResponseData.fail("邮箱不能为空");
        }
        if(userInfo.getSex() == null){
            return ResponseData.fail("性别不能为空");
        }
        if(StrUtil.isBlank(userInfo.getAddress())){
            return ResponseData.fail("地址不能为空");
        }
        if (userInfo.getPassword().length()<6) {
            return ResponseData.fail("密码长度必须大于等于6");
        }
        if (userInfo.getPassword().matches(symbol)) {
            return ResponseData.fail("密码必须包含特殊字符");
        }
        UserInfo getUser = userMapper.getUserByName(userInfo.getUserName());
        if(ObjectUtil.isNotEmpty(getUser)){
            return ResponseData.fail("用户已存在",userInfo);
        }
        userInfo.setCreateTime(new Date());
        int i = userMapper.insertUser(userInfo);
        if (i <= 0){
            return ResponseData.fail("请求失败");
        }
        return ResponseData.success(userInfo);
    }

    @Override
    public ResponseData<UserInfo> getUserByName(String userName) {
        UserInfo userInfo=userMapper.getUserByName(userName);
        return ResponseData.success(userInfo);
    }

    @Override
    public ResponseData<UserInfo> getUserByPhone(String phone) {
        UserInfo userInfo=userMapper.getUserByPhone(phone);
        return ResponseData.success(userInfo);
    }

    @Override
    public ResponseData<UserInfo> updateUser(UserInfo userInfo) {
        UserInfo getUser = userMapper.getUserByName(userInfo.getUserName());
        userInfo.setUpdateTime(new Date());
        if(ObjectUtil.isNotEmpty(getUser)){
            userMapper.updateUser(userInfo);
        }else {
            return ResponseData.fail("用户不存在");
        }
        return ResponseData.success("更新成功",userInfo);
    }

    @Override
    public ResponseData<UserInfo> deleteUserByName(String userName) {
        int i=userMapper.deleteUser(userName);
        if(i<1){
            return ResponseData.fail("删除失败");
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData<PageData<UserInfo>> page(PageQuery pageQuery) {
        return userMapper.page(pageQuery);
    }
}
